<?php
namespace app\log\model;
use think\Db;
use app\common\model\Common;
class OperLog extends Common
{
	protected $table = 'mps_operation_log';
	protected $append = ['admin_sn','nkname'];

	/**
	 * 获取列表
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param intger $page 当前页数
	 * @param intger $row 每页条数
	 * @param string $search 关键字检索条件
	 */
	public function listData($where, $order, $page, $row, $search = '')
	{
		if($where === false) return [];
		if( $row == 'limit' ){
			return $this->alias('ol')
			->join('mps_admin admin','ol.admin_id = admin.admin_id','LEFT')
			->field('ol.*,admin.admin_sn,admin.nkname')
			->where($search)->where($where)->order($order)->select();
		}
		return $this->alias('ol')
			->join('mps_admin admin','ol.admin_id = admin.admin_id','LEFT')
			->field('ol.*,admin.admin_sn,admin.nkname')
			->where($search)->where($where)->order($order)->page($page, $row)->select();
	}
	/**
	 * 获取列表总条数
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function listCount($where, $order, $search = '')
	{
		if($where === false) return 0;
		return $this->alias('ol')->join('mps_admin admin','ol.admin_id = admin.admin_id','LEFT')
		->where( $where )->where($search)->order($order)->count();
	}
	/**
	* 操作者列表
	*/
	public function getAdmin()
	{
		switch (session('admin.admin_type')) {
			case 0: $where = [];break;/*总部*/
			case 1: $where['shop_id'] = session('admin.shop_id');break;/*店长*/
			case 2: /*营业员*/
			case 3: 
			return $where['admin_id'] = session('admin.admin_id');break;/*维修员*/
			default: return [];break;
		}
		return $this->getAdminDb($where);
	}

	/**
	* 获取器
	*/
	public function getAddtimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}
	public function getAdminSnAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('admin')->where('admin_id',$data['admin_id'])->value('admin_sn');
	}
	public function getNknameAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('admin')->where('admin_id',$data['admin_id'])->value('nkname');
	}

	private function getAdminDb($where)
	{
		return Db::name('admin')->where($where)->select();
	}
}